package Librerias;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

public class Conexion {
    private Connection con;
    private ResultSet rs;
    private Statement s;
    
    public void Abrir() throws ClassNotFoundException, SQLException{
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        String url = "jdbc:derby://localhost:1527/Tienda_palonsoj2;create=true";

        con = DriverManager.getConnection(url);
        s = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                                      ResultSet.CONCUR_READ_ONLY);
    }
    public void Cerrar() throws SQLException{
        rs.close();
        s.close();
        con.close();
    }
    public ResultSet Function(String sql){
            try
            {
                    rs= s.executeQuery(sql);
                    return rs;
            }
            catch(Exception e)
            {
                   System.out.println(e.getMessage());
            }
            return null;
    }
    public void Update(String sql) throws SQLException{
                    try
            {
                    Class.forName("org.apache.derby.jdbc.ClientDriver");
                    String url = "jdbc:derby://localhost:1527/Tienda_palonsoj2;create=true";
                    Connection conn = DriverManager.getConnection(url);
                    Statement st = conn.createStatement();
                    st.executeUpdate(sql);
                    st.close();
                    conn.close();
            }
            catch(Exception e)
            {
                   System.out.println(e.getMessage());
            }
    }
    public void StoreProcedure(String sql){
            try
            {
                    Class.forName("org.apache.derby.jdbc.ClientDriver");
                    String url = "jdbc:derby://localhost:1527/Tienda_palonsoj2;create=true;";
                    Connection conn = DriverManager.getConnection(url);
                    Statement st = conn.createStatement();
                    st.execute(sql);
                    st.close();
                    conn.close();
            }
            catch(Exception e)
            {
                   System.out.println(e.getMessage());
            }
    }
}
